<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>▎{{ showTitle }}</span>
      <span class="iconfont title-icon" :style="comStyle" @click="showChoice = !showChoice">&#xe6eb;</span>
      <div class="select-con" v-show="showChoice" :style="marginStyle">
        <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handlerClick(item.key)">
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>
  
<script>
import { mapState } from 'vuex'
import { trendData } from "@/api/index";
export default {
  name: "Trend",
  data() {
    return {
      chartInstance: null,
      allData: null, //服务器获取的数据
      showChoice: false, //是否显示可选项
      choiceType: "map", //显示的数据类型
      selectTypes: [],
      titleFontSize: 0, //指明标题的字体大小
    };
  },
  watch: {
    allData: {
      deep: true,
      handler() {
        this.selectTypes = this.allData.type.filter((item) => {
          return item.key !== this.choiceType;
        });
      },
    },
    theme() {
      this.chartInstance.dispose()//销毁当前的图标
      this.initChart()//重新初始化图标
      this.screenAdapter()//完成屏幕适配
      this.updateChart()//更新图标展示
    }
  },
  computed: {
    ...mapState(['theme']),
    // selectTypes() {
    //   if (!this.allData) {
    //     return [];
    //   } else {
    //     return this.allData.type.filter((item) => {
    //       return item.key !== this.choiceType;
    //     });
    //   }
    // },
    showTitle() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.choiceType].title;
      }
    },
    //设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
      };
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize / 1.6 + "px",
      };
    },
  },
  mounted() {
    this.initChart();
    // this.getData();
    //发送数据给服务器，告诉服务器，我选择需要数据
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend',
      value: ''
    })
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  created() {
    //组件创建完成之后进行的回调函数的注册
    this.$socket.registerCallBack('trendData', this.getData)
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref, this.theme);
      const initOption = {
        grid: {
          left: "3%",
          top: "30%",
          right: "4%",
          bottom: "1%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        tooltip: {
          trigger: "axis",
        },
        legend: { left: 20, top: "15%", icon: "circle" },
        yAxis: {
          type: "value",
        },
      };
      this.chartInstance.setOption(initOption);
    },
    //ret就是服务端发送给客户端的图标的数据
    async getData(ret) {
      //获取的数据给allData赋值
      try {
        // this.allData = await trendData();
        this.allData = ret
        this.updateChart();
        //启动定时器
      } catch (e) {
        console.log(e);
      }
    },
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];
      //处理数据
      //类目轴的数据
      const timeArr = this.allData.common.month;
      //y轴的数据 series下的数据
      const valueArr = this.allData[this.choiceType].data;
      const seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: "map",
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                color: colorArr1[index],
                offset: 0,
              },
              {
                color: colorArr2[index],
                offset: 1,
              },
            ]),
          },
        };
      });
      //图例数据
      const legendArr = valueArr.map((item) => item.name);
      //处理数据
      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      //重新绘制
      this.chartInstance.resize();
    },
    handlerClick(currentType) {
      this.choiceType = currentType;
      this.updateChart();
      this.showChoice = false;
    },
  },
  beforDestroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    //组件销毁的适合进行的回调函数的取消
    this.$socket.unRegisterCallBack('trendData', this.getData)
  },
};
</script>
  
<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 1;
  color: white;

  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }

  .select-con {
    background-color: #222733;
  }
}
</style>